#!/user/bin/env node

const log = console.log,
      http = require('http');

http.createServer((req,res)=>{
  log(req.url);
  log(`${res.statusCode}${res.statusMessage} HTTP/ ${res.httpVersion}`);
  if(req.url === '/'){
    let html = '<!DOCTYPE html><html><head><title>hello</title></head><body><h1>hello world!</h1></body></html>';

    //设置请求头的两种方法
    res.setHeader('content-type','text/html;charset=utf-8');  //键值对
    res.setHeader('content-length',`${Buffer.from(html).byteLength}`);
    res.end(html.toString('utf8'));
  }else{
    res.statusCode = 404;
    res.end('Resource not found!');
  }
}).listen(8080);
